package com.chujiekeji.test.adapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.chujiekeji.test.R;
import com.chujiekeji.test.BaseApplication;
import com.chujiekeji.test.activity.IndexActivity;
import com.chujiekeji.test.activity.TaskDetailActivity;
import com.chujiekeji.test.base.BaseImageLoader;
import com.chujiekeji.test.base.BaseViewHolder;
import com.chujiekeji.test.bean.HomeBean;
import com.chujiekeji.test.bean.Task;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 适配器
 *
 * @author MapleStory
 * @ qq 1002285057
 * @ project https://gitee.com/MapStory/Shopwt-Android
 */

public class HomeListAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private static final int TYPE_ITEM   = 0;
    private static final int TYPE_FOOTER = 1;
    List<Task>  mDatas;
    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE     = 1;
    //没有加载更多 隐藏
    public static final int NO_LOAD_MORE     = 2;

    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;
    private final Activity activity;
    private OnItemClickListener onItemClickListener;
    private Context mContext;
    private LayoutInflater mInflater;
    public HomeListAdapter(Activity activity, List<Task> mDatas) {
        this.activity = activity;
        this.mDatas = mDatas;
        mContext = activity.getBaseContext();
        mInflater=LayoutInflater.from(mContext);
        this.onItemClickListener = null;
    }

    @Override
    public int getItemCount() {

        return mDatas.size()+1;

    }

    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, final int position) {
        if (holder instanceof ItemViewHolder) {

            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            Task task = mDatas.get(position);
            itemViewHolder.vNickName.setText(task.getNickName());
            itemViewHolder.vReputationLevel.setText("等级：10");
            itemViewHolder.vContent.setText(task.getTitle());
            itemViewHolder.vReward.setText("赏金："+task.getMoneyReward());
            itemViewHolder.vPublishTime.setText(task.getTime());

        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            switch (mLoadMoreStatus) {
                case PULLUP_LOAD_MORE:
                    footerViewHolder.mPbLoad.setVisibility(View.INVISIBLE);
                    footerViewHolder.mTvLoadText.setText("上拉加载更多...");
                    break;
                case LOADING_MORE:
                    footerViewHolder.mTvLoadText.setText("正加载更多...");
                    break;
                case NO_LOAD_MORE:
                    //隐藏加载更多
                    footerViewHolder.mPbLoad.setVisibility(View.INVISIBLE);
                    footerViewHolder.mTvLoadText.setText("已全部加载完");
                    break;

            }
        }

    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {
        if (viewType == TYPE_ITEM) {
            View itemView = mInflater.inflate(R.layout.temp, group, false);
            return new ItemViewHolder(itemView);
        } else if (viewType == TYPE_FOOTER) {
            View itemView = mInflater.inflate(R.layout.load_more_footview_layout, group, false);
            return new FooterViewHolder(itemView);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {

        if (position + 1 == getItemCount()) {
            //最后一个item设置为footerView
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    public interface OnItemClickListener {

        void onClick(int position, String msg);

    }


    public void AddHeaderItem(List<Task> items) {
        mDatas.clear();
        mDatas.addAll(items);
        notifyDataSetChanged();
    }

    public void AddFooterItem(List<Task> items) {
        mDatas.addAll(items);
        notifyDataSetChanged();
    }
    /**
     * 更新加载更多状态
     * @param status
     */
    public void changeMoreStatus(int status){
        mLoadMoreStatus=status;
        notifyDataSetChanged();
    }

    public class ItemViewHolder extends BaseViewHolder {
        @BindView(R.id.vNickName)
        AppCompatTextView vNickName;
        @BindView(R.id.vReputationLevel)
        AppCompatTextView vReputationLevel;
        @BindView(R.id.vContent)
        AppCompatTextView vContent;
        @BindView(R.id.vReward)
        AppCompatTextView vReward;
        @BindView(R.id.vPublishTime)
        AppCompatTextView vPublishTime;

        public ItemViewHolder(View itemView) {
            super(itemView);
            initListener(itemView);
        }

        private void initListener(View itemView) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onClick(getAdapterPosition(),"");
                }
            });
        }
    }

    public class FooterViewHolder extends BaseViewHolder {
        @BindView(R.id.pbLoad)
        ProgressBar mPbLoad;
        @BindView(R.id.tvLoadText)
        TextView mTvLoadText;
        @BindView(R.id.loadLayout)
        LinearLayout mLoadLayout;
        public FooterViewHolder(View itemView) {
            super(itemView);
            initListener(itemView);
        }
        private void initListener(View itemView) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(activity.getBaseContext(), "poistion " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
